package cs.xwi7xwa.activities;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.database.Cursor;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import cs.xwi7xwa.R;
import cs.xwi7xwa.data.ExhibitDbAdapter;
import cs.xwi7xwa.data.FactDbAdapter;
import cs.xwi7xwa.templates.BumpTemplate;

public class AddView extends BumpTemplate implements OnGesturePerformedListener
{
    private GestureLibrary gLibrary;
    private static final File gestureFile = new File(Environment
            .getExternalStorageDirectory(), "gestures");

    private FactDbAdapter dbAdapter;

    // Threshold number for the gesture matching
    private static final double SCORE_THRESH = 3.0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);

        // Load Gesture Library
        gLibrary = GestureLibraries.fromFile(gestureFile);
        if (!gLibrary.load())
        {
            // If it has failed to load library, exit
            finish();
        }

        GestureOverlayView gView = (GestureOverlayView) findViewById(R.id.addGesture);
        gView.addOnGesturePerformedListener(this);

        // Load the database
        dbAdapter = new FactDbAdapter(this);
        try
        {
            dbAdapter.open();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onGesturePerformed(GestureOverlayView view, Gesture gesture)
    {
        ArrayList<Prediction> predictions = gLibrary.recognize(gesture);

        // We want at least one prediction
        if (predictions.size() > 0)
        {
            Prediction prediction = predictions.get(0);
            // We want at least some confidence in the result
            if (prediction.score > SCORE_THRESH)
            {
                int id = Integer.parseInt(prediction.name);

                // Display a confirmation message
                Cursor cursor = dbAdapter.fetchHiddenFactExhibit(id);
                
                if (cursor != null)
                {
                    // Set the fact to visible
                    dbAdapter.setHidden(id, true);
                    
                    Toast.makeText(view.getContext(),
                        "A fact under exhibit '"
                            + cursor.getString(cursor.getColumnIndexOrThrow(ExhibitDbAdapter.KEY_NAME))
                            + "' is discovered!",
                        Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
